using BuildingBlocks.Presentation.Endpoints;
using BuildingBlocks.Presentation.Results;
using Mediator;

namespace Basket.Api.Features.Baskets.Upsert;

internal sealed class UpsertBasketEndpoint : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPut(
                BasketRoutes.Base,
                async (UpsertBasketRequest request, IMediator mediator, CancellationToken cancellationToken) =>
                {
                    var command = new UpsertBasketCommand(request.Items.ToArray());
                    var result = await mediator.Send(command, cancellationToken);
                    return result.Match(Results.Ok, ApiResponse.Problem);
                })
            .WithName("UpsertBasket")
            .WithTags("Basket");
    }
}
